#include "JSON_reader.h"
#include "weakType/weak_type.h"
#include "conditionLanguage/language.h"
#include <string>
#include <ctime> 
#include <iostream>

using namespace wolv::weakType;
using namespace wolv::condition;

using namespace std;

#define START_TIMER clock_t start,finish;double totaltime;start=clock();
#define END_TIMER  finish=clock();totaltime=(double)(finish-start)/CLOCKS_PER_SEC;printf("%d\n",(int)finish);



int allocTest();
int JsonValueAllocTest();
int parseTest();
int copyJson();
int compareTest();
int compareTest1();
int compareTest2();
int compareTest3();

int coditionTest();


int scriptTest();

int main()
{
	//allocTest();
	//JsonValueAllocTest();
	//parseTest();
	//copyJson();
	//compareTest();
	//compareTest1();
	//compareTest2();
	//compareTest3();
	//coditionTest();
	scriptTest();
}


struct Test
{
	int test;

};

int scriptTest()
{
	std::string strScript;
	std::string line;
/*
	std::cout << ">>";
	while (getline(std::cin, line))
	{
		strScript += (line + "\n");
		std::cout << ">>";
	}
*/
	strScript = "start: $System.Random_100_001 in [0,49] baidu @S1\nS1: $System.Random_100 in [50,99] alimama fail";

	Parser parser;
	Script script;

	SymbolImporter * pImporter = new SystemImporter();
	script.LinkSymbol(pImporter);

	int result = parser.Parse(strScript, script);
	if (parser.GetLastError() != 0)
	{
		printf("%s", parser.GetLastErrorMessage().c_str());
		return 0;
	}

	int i = 10;
	while (i--)
	{
		string runResult;
		runResult = script.Exec(NULL);

		if (script.GetLastError() != 0)
		{
			printf("%s", script.GetLastErrorMessage().c_str());
			return 0;
		}
		
		printf("%s", runResult.c_str());

		if (runResult == "fail")
		{
			printf("error");
		}
	}


	getchar();

	return 0;
};

int coditionTest()
{
	START_TIMER

	Parser parser;
	Script script;

	SymbolImporter * pImporter = new SystemImporter();
	script.LinkSymbol(pImporter);

	string str = "start: $System.Random_100 in [0,50] @s1 Fail_on_start\n\
		s1: $System.Random_100 in [0,50] @s2 Fail_on_s1\n\
		s2: $System.Random_100 in [0,50] @s3 Fail_on_s2\n\
		s3: $System.Random_100 in [0,50] @s4 Fail_on_s3\n\
		s4: $System.Random_100 in [0,50] end Fail_on_s4\n\
		";
	

	int result = parser.Parse(str, script);
	if (parser.GetLastError() != 0)
	{
		printf("%s", parser.GetLastErrorMessage().c_str());
		return 0;
	}
	


	string runResult;
	int i = 1;
	while (i--)
	{
		runResult = script.Exec(NULL);
		
		if (script.GetLastError() != 0)
		{
			printf("%s", script.GetLastErrorMessage().c_str());
			return 0;
		}
		printf("%s", runResult.c_str());
	}

	END_TIMER
	return 0;
}

int compareTest()
{

	Value testInt = -123.3;
	Value testStr = "123";

	printf("%s\n",Value::TypeName(testStr.GetType()).c_str());
	testStr.StringTypeConvert();
	printf("%s\n",Value::TypeName(testStr.GetType()).c_str());

	return 0; 
}

int compareTest3()
{
	Value testArray;
	testArray.append(1);
	testArray.append("2");
	testArray.append("3.1");
	testArray.append("abc");

	Value test = "1";

	printf("%d\n",test.in(testArray));
	test.StringTypeConvert();
	printf("%d\n",test.in(testArray));

	return 0; 
}


int compareTest1()
{
	Test t;
	Value testInt = 123;

	t.test = testInt.asInt();

	START_TIMER;
	int i = 1000000000;

	int j;
	while (i--) 
	{
		j = t.test;
		j += j;
	}


	printf("%d\n",j);
	END_TIMER;
	return 0; 
}


int compareTest2()
{
	Value testInt = 123;

	START_TIMER;
	int i = 1000000000;

	int j;
	while (i--) 
	{
		j = testInt.asInt();
		j += j;
	}


	printf("%d\n",j);
	END_TIMER;
	return 0; 
}

int JsonValueAllocTest()
{
	START_TIMER;
	int i = 1000000;

	std::map<int,int> test;

	while (i--) 
	{
		//std::string test3;
		//std::vector<int> test2;
		//std::map<int,int> test;

		Json::Value data;
	}


	END_TIMER;


	return 0;
}

int allocTest()
{
	START_TIMER;

	int * p;

	int i = 1000000;

	string str = "{\"a\":\"b\",\"b\":\"b\",\"c\":\"b\",\"d\":\"b\",\"e\":\"b\"}";

	Json::Value tmp;

	while (i--) 
	{
		Json::Reader reader, reader2;
		reader.parse(str.c_str(), str.size(), tmp);
		reader2.parse(str.c_str(), str.size(), tmp);
	}

	END_TIMER;
	return 0;
}

int parseTest()
{
	START_TIMER;

	//string str = "{\"1\":\"a\",\"b\":{\"b\":\"b\"}}";
	
	string str = "{\"a\":\"b\",\"b\":\"b\",\"c\":\"b\",\"d\":\"b\",\"e\":\"b\",\"f\":{\"a\":\"b\",\"b\":\"b\",\
\"c\":\"b\",\"d\":\"b\",\"e\":\"b\"},\"g\":{\"a\":\"b\",\"b\":\"b\",\"c\":\"b\",\"d\":\"b\",\"e\":\"b\"},\
\"h\":{\"a\":\"b\",\"b\":\"b\",\"c\":\"b\",\"d\":\"b\",\"e\":\"b\",\"f\":{\"a\":\"b\",\"b\":\"b\",\
\"c\":\"b\",\"d\":\"b\",\"e\":\"b\"},\"g\":{\"a\":\"b\",\"b\":\"b\",\"c\":\"b\",\"d\":\"b\",\"e\":\"b\"}}}";

	Json::Value tmp;
	Json::Reader reader;

	int i = 1000;
	while (i--) 
	{
		int result = reader.parse(str.c_str(), str.size(), tmp);
	}

	END_TIMER;
	return 0;
}


int copyJson()
{
	START_TIMER;

	Json::Value tmp;
	Json::Reader reader;
	Json::Value copy;
	string str = "{\"k1\":{\"k1\":{\"k1\":{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":3,\"k4\":4,\"k5\":[\"v1\",2,3],\"k6\":{\"kk1\":1}},\"k2\":\"v2\",\"k3\":3,\"k4\":4,\"k5\":[\"v1\",2,3],\"k6\":{\"kk1\":1}},\"k2\":\"v2\",\"k3\":3,\"k4\":4,\"k5\":[\"v1\",2,3],\"k6\":{\"kk1\":1}},\"k2\":\"v2\",\"k3\":3,\"k4\":4,\"k5\":[\"v1\",2,3],\"k6\":{\"kk1\":1}}";

	int result = reader.parse(str.c_str(), str.size(), tmp);

	int i = 1000, a = 0;
	while (i--) 
	{
		copy = tmp;
	}

	END_TIMER;
	return 0;
}
